﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using ebay.Dal;

public partial class UI_Mail_Users : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            String act = Request["act"];
            String username = Request["username"];
            String signature = Request["signature"];

            if (String.IsNullOrEmpty(act))
                Response.Redirect("Config.aspx");

            switch (act) {
                case "i":
                    break;
                case "u":
                    this.txtUsername.Text = username;
                    this.txtSignature.Text = signature;
                    break;
            }
        }
    }

    protected void ButtonSave_Click(object sender, EventArgs e) {
        String act = Request["act"];
        String id = Request["id"];
        String username = txtUsername.Text;
        String signature = txtSignature.Text;
        String sqlString = String.Empty;
        SqlParameter[] parameters = null;

        switch (act.ToLower()) {
            case "i": {
                sqlString = "INSERT MailUsers(Username,Signature) VALUES(@Username,@Signature)";
                    parameters = new SqlParameter[] {
                        new SqlParameter() { ParameterName = "@Username", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = username },
                        new SqlParameter() { ParameterName = "@Signature", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = signature }
                    };

                    int val = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                    if (val > 0) {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "Success", "<script>alert('新增成功!');window.location.href='Config.aspx'</script>");
                    }
                }
                break;
            case "u": {
                sqlString = "UPDATE MailUsers SET Username=@Username,Signature=@Signature WHERE ID=@ID";
                    parameters = new SqlParameter[] {
                        new SqlParameter() { ParameterName = "@ID", SqlDbType = SqlDbType.Int, Value = id },
                        new SqlParameter() { ParameterName = "@Username", SqlDbType = SqlDbType.VarChar, Size = 50, Value = username },
                        new SqlParameter() { ParameterName = "@Signature", SqlDbType = SqlDbType.VarChar, Size = 50, Value = signature }
                    };
                    int val = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                    if (val > 0) {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "Success", "<script>alert('修改成功!');window.location.href='Config.aspx'</script>");
                    }
                }
                break;
            default:
                return;
        }
    }
}